package com.hhl.flower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhl.flower.mapper.HistoryMapper;
import com.hhl.flower.pojo.entity.Flower;
import com.hhl.flower.pojo.entity.History;
import com.hhl.flower.pojo.vo.HistoryVO;
import com.hhl.flower.service.FlowerService;
import com.hhl.flower.service.HistoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
* @author Liubuzhu
* @description 针对表【history】的数据库操作Service实现
* @createDate 2024-06-20 09:55:51
*/
@Service
public class HistoryServiceImpl extends ServiceImpl<HistoryMapper, History>
    implements HistoryService{
	@Autowired
	private HistoryMapper historyMapper;
	@Autowired
	private FlowerService flowerService;

	public List<HistoryVO> listHistoryFlowerOrderByTime(Integer userId) {

		QueryWrapper<History> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("u_id", userId);
		queryWrapper.orderByDesc("create_time");
		List<History> histories = historyMapper.selectList(queryWrapper);


		List<HistoryVO> historyVOList =  new ArrayList<>();
		for (History history : histories) {
			HistoryVO historyVO = new HistoryVO();
			Flower flower = flowerService.getById(history.getFId());
			BeanUtils.copyProperties(flower, historyVO);
			historyVO.setId(history.getId());
			historyVO.setCreateTime(history.getCreateTime());
            historyVOList.add(historyVO);
		}

		return historyVOList;
	}

	public void addToHistory(Integer userId, Integer flowerId) {
		History history = new History();
        history.setUId(userId);
        history.setFId(flowerId);
		history.setCreateTime(LocalDateTime.now());
        historyMapper.insert(history);
	}


}




